#include"UnitSelector.h" 

#include"Unit.h"
#include"UnitSelectionWidget.h"

UnitSelector::UnitSelector(QList<Unit*> &teamA, QList<Unit*> &teamB, Map *map):m_teamA(teamA), m_teamB(teamB){
	m_map = map;
	m_layout = new QVBoxLayout(this);
	setLayout(m_layout);
	
	m_label = new QLabel("Please select the teams for the AI simulation", this);
	m_layout->addWidget(m_label);
	
	m_teamsWidget = new QWidget(this);
	m_layout->addWidget(m_teamsWidget);
	
	m_teamsLayout = new QHBoxLayout(m_teamsWidget);
	m_teamsWidget->setLayout(m_teamsLayout);
	
	m_teamAWidget = new QWidget(m_teamsWidget);
	m_teamsLayout->addWidget(m_teamAWidget);
	
	m_teamALayout = new QVBoxLayout(m_teamAWidget);
	m_teamAWidget->setLayout(m_teamALayout);
	
	m_teamASelection.push_back( new UnitSelectionWidget(m_teamAWidget) );
	m_teamALayout->addWidget(m_teamASelection[0]);
	connect(m_teamASelection[0], SIGNAL(deleteThis()), this, SLOT(slotTeamADelete()));
	
	m_teamAMore = new QPushButton("More", m_teamAWidget);
	m_teamALayout->addWidget(m_teamAMore);
	connect(m_teamAMore, SIGNAL(clicked()), this, SLOT(slotTeamAMore()));
	
	m_teamBWidget = new QWidget(m_teamsWidget);
	m_teamsLayout->addWidget(m_teamBWidget);
	
	m_teamBLayout = new QVBoxLayout(m_teamBWidget);
	m_teamBWidget->setLayout(m_teamBLayout);
	
	m_teamBSelection.push_back( new UnitSelectionWidget(m_teamBWidget) );
	m_teamBLayout->addWidget(m_teamBSelection[0]);
	connect(m_teamBSelection[0], SIGNAL(deleteThis()), this, SLOT(slotTeamBDelete()));
	
	m_teamBMore = new QPushButton("More", m_teamBWidget);
	m_teamBLayout->addWidget(m_teamBMore);
	connect(m_teamBMore, SIGNAL(clicked()), this, SLOT(slotTeamBMore()));
	
	m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, Qt::Horizontal, this);
	connect(m_buttons, SIGNAL(accepted()), this, SLOT(slotAccepted()));
	connect(m_buttons, SIGNAL(rejected()), this, SLOT(slotRejected()));
	m_layout->addWidget(m_buttons);
}

UnitSelector::~UnitSelector(){
	
}

bool UnitSelector::getTeams( QList<Unit*> &teamA, QList<Unit*> &teamB, Map *map ){
	UnitSelector *selector = new UnitSelector(teamA, teamB, map);
	int ret = selector->exec();
	delete selector;
	return ret != 0;
}

void UnitSelector::slotAccepted(){
	for(int i = 0; i < m_teamASelection.size(); i++)
		m_teamA.push_back(m_teamASelection[i]->getUnit(m_map, TEAM_A));
	for(int i = 0; i < m_teamBSelection.size(); i++)
		m_teamB.push_back(m_teamBSelection[i]->getUnit(m_map, TEAM_B));
	done(1);
}

void UnitSelector::slotRejected(){
	done(0);
}

void UnitSelector::slotTeamAMore(){
	if(m_teamASelection.size() < 8){
		UnitSelectionWidget *widget = new UnitSelectionWidget(m_teamAWidget);
		if(m_teamASelection.size()){
			widget->setAge(m_teamASelection.last()->age());
			widget->setType(m_teamASelection.last()->type());
		}
		m_teamALayout->insertWidget(m_teamASelection.size(), widget);
		m_teamASelection.push_back(widget);
		connect(widget, SIGNAL(deleteThis()), this, SLOT(slotTeamADelete()));
	}
}

void UnitSelector::slotTeamBMore(){
	if( m_teamBSelection.size() < 8 ){
		UnitSelectionWidget *widget = new UnitSelectionWidget(m_teamBWidget);
		if(m_teamBSelection.size()){
			widget->setAge(m_teamBSelection.last()->age());
			widget->setType(m_teamBSelection.last()->type());
		}
		m_teamBLayout->insertWidget(m_teamBSelection.size(), widget);
		m_teamBSelection.push_back(widget);
		connect(widget, SIGNAL(deleteThis()), this, SLOT(slotTeamBDelete()));
	}
}

void UnitSelector::slotTeamADelete(){
	if(m_teamALayout->count() == 1)
		return;
	QWidget *s = (QWidget*)sender();
	for(int i = 0; i < m_teamALayout->count(); i++){
		if( m_teamALayout->itemAt(i)->widget() == s ){
			m_teamALayout->removeWidget(s);
			s->deleteLater();
			m_teamASelection.removeOne((UnitSelectionWidget*)s);
			return;
		}
	}
}

void UnitSelector::slotTeamBDelete(){
	if(m_teamBLayout->count()==1)
		return;
	QWidget *s = (QWidget*)sender();
	for(int i = 0; i < m_teamBLayout->count(); i++){
		if( m_teamBLayout->itemAt(i)->widget() == s ){
			m_teamBLayout->removeWidget(s);
			s->deleteLater();
			m_teamBSelection.removeOne((UnitSelectionWidget*)s);
			return;
		}
	}
}
